﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;

namespace DotNet.Utilities
{
    public class DelegateProxy
    {
        public DelegateProxy(Control c)
        {
            this.c = c;
        }

        public DelegateProxy()
        {

        }

        private Control c;

        public Control C
        {
            get { return c; }
            set { c = value; }
        }

        private delegate void ControlmsgAddHander(Control c,string msg);
        private delegate void ControlmsgAddHander2(Control c, ListViewItem msg);
        private delegate void ControlEnableHander(Control c, bool enable);
        private delegate void ListBoxmsgAddHander(ListBox l,string msg);
       
        private delegate void ControlSortHander2(Control c);
        private delegate void PictureBoxMsgAddHander( Image BackGroudImage, int width, int height);
        private delegate void PictureBoxGetpHander();
       
        private delegate string GetMsgHander(Control c);
        private delegate void ListviewAddRangeHandler(string[] data, ListView lv);
        private delegate void ListviewclearHander(ListView lv);
        private delegate void DataRowViewclearHander(DataGridView lv);
        private delegate void DataRowviewRowaddhander(DataGridView lv, string[] data);
        private delegate void DataRowColumnAddHander(DataGridView dv, string colname, string coltext);
        private delegate void DataGridViewChangeWithHander(DataGridView dv, int with, int colindex);
        private delegate void ProGressMaxSetHander(ProgressBar pb, int maxvalue);
        private delegate void ProGressChangeHander(ProgressBar pb, int changevalue);


        private void ProGressChangeInstrance(ProgressBar pb, int changevalue)
        {
            pb.Value += changevalue;
        }

        public void ProGressChange(ProgressBar pb, int changevalue)
        {
            if (pb.InvokeRequired)
            {
                ProGressChangeHander msgadd = new ProGressChangeHander(ProGressChangeInstrance);
                pb.Invoke(msgadd, new object[] { pb, changevalue });
            }
            else
            {
                ProGressChangeInstrance(pb, changevalue);
            }
        }

        private void ProGressMaxSetInstrance(ProgressBar pb, int maxvalue)
        {
            pb.Maximum = maxvalue;
        }
        public void ProGreeMaxSet(ProgressBar pb, int maxvalue)
        {
            if (pb.InvokeRequired)
            {
                ProGressMaxSetHander msgadd = new ProGressMaxSetHander(ProGressMaxSetInstrance);
                pb.Invoke(msgadd, new object[] { pb, maxvalue });
            }
            else
            {
                ProGressMaxSetInstrance(pb, maxvalue);
            }
        }

        private void DataRowChangewidthInstrance(DataGridView dv, int with,int colindex)
        {
            dv.Columns[colindex].Width = with;
        }

        public void DataRowChangewidth(DataGridView dv, int with, int colindex)
        {
            if (dv.InvokeRequired)
            {
                DataGridViewChangeWithHander msgadd = new DataGridViewChangeWithHander(DataRowChangewidthInstrance);
                dv.Invoke(msgadd, new object[] { dv, with, colindex });
            }
            else
                DataRowChangewidthInstrance(dv, with, colindex);
        }

        private void DataRowColumnAddInstrance(DataGridView dv, string colname, string coltext)
        {
            dv.Columns.Add(colname, coltext);
        }

        public void DataRowColumnAdd(DataGridView dv, string colname, string coltext)
        {
            try
            {
                if (dv.InvokeRequired)
                {
                    DataRowColumnAddHander msgadd = new DataRowColumnAddHander(DataRowColumnAddInstrance);
                    dv.Invoke(msgadd, new object[] { dv, colname, coltext });
                }
                else
                    DataRowColumnAddInstrance(dv, colname, coltext);
            }
            catch(Exception ee)
            {
                Console.WriteLine(ee.Message);
            }
        }

        private void DataRowviewRowaddInstrance(DataGridView lv, string[] data)
        {
            
            lv.Rows.Add(data);
        }

        public void DataRowviewRowAdd(DataGridView lv, string[] data)
        {
            if (lv.InvokeRequired)
            {
                DataRowviewRowaddhander msgadd = new DataRowviewRowaddhander(DataRowviewRowaddInstrance);

                lv.Invoke(msgadd, new object[] {lv,data });
            }
            else
                DataRowviewRowaddInstrance(lv, data);
        }

        private void DataRowViewclearInstrance(DataGridView dg)
        {
            dg.Rows.Clear();
        }

        public void DataRowViewclear(DataGridView dg)
        {
            if (dg.InvokeRequired)
            {
                DataRowViewclearHander msgadd = new DataRowViewclearHander(DataRowViewclearInstrance);
                dg.Invoke(msgadd, new object[] {dg });
            }
            else
            {
                DataRowViewclearInstrance(dg);
            }
        }

        private void ListviewclearInstrance(ListView lv)
        {
            lv.Items.Clear();
        }

        public void Listviewclear(ListView lv)
        {
            if (lv.InvokeRequired)
            {
                ListviewclearHander msgadd = new ListviewclearHander(ListviewclearInstrance);
                lv.Invoke(msgadd, new object[] { lv});
            }
            else
                ListviewclearInstrance(lv);
        }

        private void ListviewAddRangeInstrance(string[] data, ListView lv)
        {
            ListViewItem[] lvlist = new ListViewItem[data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                lvlist[i] = new ListViewItem(data[i]);
            }
            lv.Items.AddRange(lvlist);
        }

        public void ListviewAddRange(string[] data, ListView lv)
        {
            if (lv.InvokeRequired)
            {
                ListviewAddRangeHandler msgadd = new ListviewAddRangeHandler(ListviewAddRangeInstrance);
                lv.Invoke(msgadd, new object[] {data,lv });
            }
            else
                ListviewAddRangeInstrance(data, lv);
        }

        private string GetMsgInstrance(Control c)
        {
            return c.Text;
        }

        public string GetMsg(Control c)
        {
            if (c.InvokeRequired)
            {
                GetMsgHander msgadd = new GetMsgHander(GetMsgInstrance);
                return  c.Invoke(msgadd, new object[] { c}).ToString();
            }
            else
            {
                return GetMsgInstrance(c);
            }
        }

  

        private void ControlmsgAddInstance(Control c, string msg)
        {
            if (this.c == null)
                this.c = c;
            if (c is ListView)
            {
                
            }
            else if (c is ProgressBar)
            {
                ((ProgressBar)c).Value = Convert.ToInt32(msg);
            }
            else
            {
                c.Text = msg;
            }
            
        }

      
        private void ControlSortInstance(Control c)
        {
            if (c is ListView)
                ((ListView)c).Sort();
        }

      

        public void ControlSort(Control c)
        {
            if (c.InvokeRequired)
            {
                ControlSortHander2 msgadd = new ControlSortHander2(ControlSortInstance);
                c.Invoke(msgadd, new object[] { c });
            }
            else
                ControlSort(c);
        }

        private void ControlmsgAddInstance(Control c, ListViewItem lvi)
        {
            if (this.c == null)
                this.c = c;
            if (c is ListView)
            {
                ((ListView)c).Items.Add(lvi);
            }
        }

        private void ControlEnableInstance(Control c, bool enable)
        {
            if (this.c == null)
                this.c = c;
            c.Enabled = enable;
        }

        private void PictureBoxInstance()
        {
            PictureBox x = (PictureBox)c;
            for (int i = 0; i < x.BackgroundImage.Width; i++)
            {
                for (int y = 0; y < x.BackgroundImage.Height; y++)
                {
                    Color n = ((Bitmap)x.BackgroundImage).GetPixel(i, y);

                    byte d = n.R;
                    if (d < 100)
                    {
                        ((Bitmap)x.BackgroundImage).SetPixel(i, y, Color.White);
                    }
                    else
                    {
                        ((Bitmap)x.BackgroundImage).SetPixel(i, y, Color.Black);
                    }

                };
            }
        }

        private void ListBoxmsgAddInstance(ListBox l, string msg)
        {
            l.Items.Add(msg);
        }

        private void PictureBoxMsgAddInstance( Image BackGroudImage, int width, int height)
        {
            PictureBox p = (PictureBox)c;
            p.BackgroundImage = BackGroudImage;
            p.Width = width;
            p.Height = height;
        }

        public void ControlmsgAdd(Control c, string msg)
        {
            if (c.InvokeRequired)
            {
                try
                {
                    ControlmsgAddHander msgadd = new ControlmsgAddHander(ControlmsgAddInstance);
                    c.Invoke(msgadd, new object[] { c, msg });
                }
                catch(Exception ee)
                {
                    Console.WriteLine(ee.Message);
                }
            }
            else
            {
                ControlmsgAddInstance(c, msg);
            }
        }

        public void ControlEnable(Control c, bool enable)
        {
            if (c.InvokeRequired)
            {
                ControlEnableHander msgadd = new ControlEnableHander(ControlEnableInstance);
                c.Invoke(msgadd, new object[] {c,enable });
            }
            else
            {
                ControlEnableInstance(c, enable);
            }
        }

        public void ControlmsgAdd(Control c, ListViewItem vi)
        {
            if (c.InvokeRequired)
            {
                ControlmsgAddHander2 msgadd = new ControlmsgAddHander2(ControlmsgAddInstance);
                c.Invoke(msgadd, new object[] {c,vi });
            }
            else
            {
                ControlmsgAddInstance(c, vi);
            }
        }

        public void ListBoxmsgAdd(ListBox l, string msg)
        {
            if (l.InvokeRequired)
            {
                ListBoxmsgAddHander msgadd = new ListBoxmsgAddHander(ListBoxmsgAddInstance);
                l.Invoke(msgadd, new object[] { l, msg });
            }
            else
            {
                ListBoxmsgAddInstance(l, msg);
            }
        }

        public void PictureBoxMsgAdd(Image BackGroudImage, int width, int height)
        {
            if (c.InvokeRequired)
            {
                PictureBoxMsgAddHander msgadd = new PictureBoxMsgAddHander(PictureBoxMsgAddInstance);
                c.Invoke(msgadd, new object[] {BackGroudImage,width,height });
            }
            else
            {
                PictureBoxMsgAddInstance(BackGroudImage, width, height);

            }
        }

        public void PictureBoxGetp()
        {
            PictureBoxGetpHander hander = new PictureBoxGetpHander(PictureBoxInstance);
            if (c.InvokeRequired)
            {
                
                c.Invoke(hander, new object[] { });
            }
            else
            {
                PictureBoxInstance();
            }
        }
    }
}
